<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.admin.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

function get_description($pID, $full_id, $type) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	$field = check_type($type);
		
	if(!empty($field['name'])){
		$get_desc = $db->db_query("SELECT 
										".$field['name']." 
									FROM 
										".TABLE_PRODUCTS_DESCRIPTION."
									WHERE
										products_id = '".$pID."'
									AND
										language_id = '".(int)$_SESSION['languages_id']."'");
												
		if(!empty($get_desc->fields[$field['name']])) {
			
			$hidden  = draw_hidden_field('products_id', $pID);
			$hidden .= draw_hidden_field('full_id', $full_id);
			$hidden .= draw_hidden_field('type', $type);
			$ajax->append('dialog', 'innerHTML', $hidden);
		}

	} else
		$ajax->assign('$("#dialog").dialog("close")');
		
	return $ajax;
}

function save_desc($pID, $content, $type, $full_id) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	$field = check_type($type);
	
	if(!empty($field['name'])) {
	
		$content = trim(stripslashes($content));
	
		$db->db_query("UPDATE 
							".TABLE_PRODUCTS_DESCRIPTION."
						SET
							".$field['name']." = '".$content."'
						WHERE
							products_id = '".$pID."'
						AND
							language_id = '".(int)$_SESSION['languages_id']."' ");
		
		if($field['lenght'] > 1)
			$content = truncate($content, $field['lenght']);
		$ajax->script('$("#'.$full_id.' > .product_listing_gallery_detail_cat").html("'.$content.'")');
	}
	$ajax->script('$(".ui-dialog").remove()');
	$ajax->script('$("#dialog").remove()');
	return $ajax;
}

function get_name($pID, $full_id) {
	global $db;
	$ajax = new xajaxResponse();
	
	$get_name = $db->db_query("SELECT 
									products_name 
								FROM 
									".TABLE_PRODUCTS_DESCRIPTION."
								WHERE
									products_id = '".$pID."'
								AND
									language_id = '".(int)$_SESSION['languages_id']."'");
	
	$js = 'set_dialog();
			inner  = \'<link rel="stylesheet" type="text/css" href="admin/includes/javascript/ui/jquery-ui-1.8.16.custom.css" media="screen, projection">\';
			inner += \'<input type="text" class="edit_products_name" name="products_name" value="'.$get_name->fields['products_name'].'" style="width:98%" />\';
			$(\'#dialog\').html(inner);
			$(\'#dialog\').dialog({
				autoOpen:false,
				show:"fade",
				title:\'Namen bearbeiten\',
				width:300,
				height: 130,
				modal:true,
				position:[\'center\',\'center\'],
				buttons:{
					Speichern:function() {
						var full_id=$(\'input[name=full_id]\').val();
						var pID=$(\'input[name=products_id]\').val();
						var name = $(\'.edit_products_name\').val();
						xajax_save_name(pID, name, full_id);
						$(this).dialog(\'close\');
					},Abbrechen:function(){
						$(this).dialog(\'close\');
						$(".ui-dialog").remove();
						$("#dialog").remove();
					}
				}
			});
			$("#dialog").dialog(\'open\');';
	$ajax->script($js);
	
	$hidden  = draw_hidden_field('products_id', $pID);
	$hidden .= draw_hidden_field('full_id', $full_id);
	$ajax->script('$(\'#dialog\').append(\''.$hidden.'\')');
	
	return $ajax;
}

function save_name($pID, $name, $full_id) {
	global $db;
	
	$ajax = new xajaxResponse();

	$db->db_query("UPDATE 
						".TABLE_PRODUCTS_DESCRIPTION."
					SET
						products_name = '".$name."'
					WHERE
						products_id = '".$pID."'
					AND
						language_id = '".(int)$_SESSION['languages_id']."' ");
	
	if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
		require_once(DIR_FS_INC.'inc.seo_url.php');
		!$seo_url ? $seo_url = new SeoUrl() : false;
		$seo_url->updateSeoDBTable('product', $pID);
		$url = href_link(FILENAME_PRODUCT_INFO, 'products_id='.$pID);
		$ajax->script('$("#'.$full_id.' > h2 > a").attr("href", "'.$url.'")');
		$ajax->script('$("#'.$full_id.' > h2 > a").attr("title", "'.$name.'")');
		$ajax->script('$("#'.$full_id.' > .product_listing_gallery_image > a").attr("href", "'.$url.'")');
		$ajax->script('$("#'.$full_id.' > .product_listing_gallery_image > a").attr("title", "'.$name.'")');
	}
		
	$ajax->script('$("#'.$full_id.' > h2 > a").html("'.$name.'")');
	$ajax->script('$(".ui-dialog").remove()');
	$ajax->script('$("#dialog").remove()');
	
	return $ajax;
}

function check_type($type) {
	global $db;
	
	$check_for_desc = $db->db_query("SELECT 
										p_short_desc,
										p_short_desc_lenght,
										p_long_desc,
										p_long_desc_lenght
									FROM
										".TABLE_PRODUCTS_LISTINGS."
									WHERE
										list_name = 'product_listing_".$type."'");
	
	
	
	if($check_for_desc->fields['p_short_desc'] == 1)
		return array('name' => 'products_short_description', 'lenght' => $check_for_desc->fields['p_short_desc_lenght']);
	elseif($check_for_desc->fields['p_long_desc'] == 1)
		return array('name' => 'products_description', 'lenght' => $check_for_desc->fields['p_long_desc_lenght']);
	else
		return;
}

$xajax->registerFunction("get_description");
$xajax->registerFunction("save_desc");
$xajax->registerFunction("get_name");
$xajax->registerFunction("save_name");
$xajax->getJavascriptConfig();
?>